package cz.proks.easyhelpdesk.gwt.restapi;

import java.util.List;

import org.jsonmaker.gwt.client.Jsonizer;
import org.spiffyui.client.rest.RESTCallback;
import org.spiffyui.client.rest.RESTility;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONValue;

import cz.proks.easyhelpdesk.api.config.Configuration;
import cz.proks.easyhelpdesk.api.model.HelpdeskMember;
import cz.proks.easyhelpdesk.api.model.HelpdeskMemberJsonizer;
import cz.proks.easyhelpdesk.api.model.HelpdeskRole;
import cz.proks.easyhelpdesk.gwt.utils.JsonizerUtils;
import cz.proks.easyhelpdesk.gwt.utils.RestUtils;

public class HelpdeskMemberRestApi extends BaseRestApi<HelpdeskMember>{
    private final static Jsonizer JSONIZER = GWT.create(HelpdeskMemberJsonizer.class);

    @Override
    protected String getBaseRestDomainUrlPart() {
        return Configuration.WS.HELPDESK_MEMBER_BASE_URL;
    }

    @Override
    protected Jsonizer getDomainJsonizer() {
        return JSONIZER;
    }     
    
    public void getForUserAndHelpdesk(Long userId, Long helpdeskId, final DomainCallback<HelpdeskMember> callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/getForUser/" + userId + "/ForHelpdesk/" + helpdeskId, new RestUtils.Callback(callback) {
            
            @Override
            public void onSuccess(JSONValue val) {
                HelpdeskMember helpdeskMember = JsonizerUtils.getEntity(JSONIZER, val);
                callback.onDomainSuccess(helpdeskMember);
            }
        });
    }
    
    public void listForUser(Long userId, final DomainListCallback<HelpdeskMember> callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/listForUser/" + userId, new RestUtils.Callback(callback) {
            
            @Override
            public void onSuccess(JSONValue val) {
                List<HelpdeskMember> helpdeskMembers = JsonizerUtils.getListFromJsonArray(val, JSONIZER);
                callback.onDomainListSuccess(helpdeskMembers);
            }
        });
    }
    
    public void listForHelpdesk(Long helpdeskId, final DomainListCallback<HelpdeskMember> callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/listForHelpdesk/" + helpdeskId, new RestUtils.Callback(callback) {
            
            @Override
            public void onSuccess(JSONValue val) {
                List<HelpdeskMember> helpdeskMembers = JsonizerUtils.getListFromJsonArray(val, JSONIZER);
                callback.onDomainListSuccess(helpdeskMembers);
            }
        });
    }
        
    public void addHelpdeskRole(Long id, HelpdeskRole helpdeskRole, final RESTCallback callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/" + id + "/addHelpdeskRole/" + helpdeskRole.toString(), null, RESTility.PUT, callback);
    }
    
    public void removeHelpdeskRole(Long id, HelpdeskRole helpdeskRole, final RESTCallback callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/" + id + "/removeHelpdeskRole/" + helpdeskRole.toString(), null, RESTility.DELETE, callback);
    }
}
